$(document).ready( function() {
	$('#msgbox').hide();
}); // hide msgbox hack
// global for size of board
var gamesize = 12;
var done = new Array(); // array of 'flooded' elements
var allPcs = new Array();
// global vars for move count
var ingame = false;
var lastMove;
var maxMoves;
var moveCount;
var theSize; // global size
var theSizeString; // global size

// start/build ************************************
function generateBoard(k)
{
/*	switch(theSize)
	{
		case 0:
			theSizeString = ' Psmall';
			break;
		case 1:
			theSizeString = ' Pmedium';
			break;
		case 2:
			theSizeString = ' Plarge';
			break;
		default:
			theSizeString = ' Psmall';
	}*/
	for (var i = 0; i < gamesize; i++)
	{//ycoord
		for (var j = 0; j < gamesize; j++)
		{//xcoord
			makediv(i,j,k);	
		}
	}
	$('#start').css('display' , 'none'); // remove start button
	$('#restart').css('display' , 'block'); // show restart
}
function makediv(i,j)
{
	var board = document.getElementById("gameboard");
	var newdiv = document.createElement("div");
	var theID = j + 'p' + i; // id to use in arrays for comparison against doneEle's
	newdiv.id = theID;
	var random = Math.floor(Math.random()*6);
	switch(random)
	{
		// build initial color arrays to be used, contain all elements of a color
		// give class names
		case 0:
			newdiv.className = 'pieces green' + theSizeString;
			break;
		case 1:
			newdiv.className = 'pieces purple' + theSizeString;
			break;
		case 2:
			newdiv.className = 'pieces yellow' + theSizeString;
			break;
		case 3:
			newdiv.className = 'pieces red' + theSizeString;
			break;
		case 4:
			newdiv.className = 'pieces orange' + theSizeString;
			break;
		case 5:
			newdiv.className = 'pieces blue' + theSizeString;
			break;
		default:
			return;
	}
	board.appendChild(newdiv);
	allPcs.push(theID);
	if (i == 0 && j == 0)
	{ // get first boxes color so they cant play it first move and waste a move
		lastMove = newdiv.className.replace('/^\s+|\s+|pieces/g', ''); 
		// extract color and whitespace
	}
}
function startGame(size)
{ // size is always int
		switch(size)
		{
			case 0:
				theSize = 0;
				theSizeString = ' Psmall';
				gamesize = 12;
				maxMoves = 22;
				break;
			case 1:
				theSize = 1;
				theSizeString = ' Pmedium';
				gamesize = 18;
				maxMoves = 30;
				break;
			case 2:
				theSize = 2;
				theSizeString = ' Plarge';
				gamesize = 22;
				maxMoves = 36;
				break;
			default:
				theSize = 0;
				gamesize = 12;
				maxMoves = 22;
				break;
		}

	lastMove = null;
	moveCount = 0;
	$('#maxmoves').html(maxMoves); // place maxmoves onto the page
	$('#playermoves').html(moveCount); // place maxmoves onto the page
	generateBoard();
	done.push('#0p0'); // first element in top left corner, its always 'done'
	updateDone();
	ingame = true;
	for (var i = 0; i < done.length; i++)
	{
		colorsMatch(done[i]);// call the initial join method to group ele's touching p00
	}
}
// start/build ************************************

// restart ****************************************
function restartGame(flag)
{
	killBoard();
	startGame(theSize);
	if (flag)
		hider();
}
function killBoard()
{
	var board = document.getElementById('gameboard');
	for (var i = 0; i < allPcs.length; i++)
	{
		var pc = allPcs[i].replace('#', '');
		var ele = document.getElementById(pc);
		board.removeChild(ele);
	}
	allPcs.length = 0; // clear allPcs arr
	done.length = 0; // clear done arr
	ingame = false;
	maxMoves = 0;
	moveCount = 0;
	gamesize = 0;
	lastMove = null;
}
// restart ****************************************

// gameplay ***************************************
function updateDone()
{
	for (var j = 0; j < done.length; j++)
	{// each element in done array
		if (!($(done[j]).hasClass('done')))
		{// if element doesnt have 'done' class, add it.
			$(done[j]).addClass('done');
		}
	}
}
function colorsMatch (doneEle)
{
	var tempID = doneEle.replace('#', '');
	tempID = tempID.split('p');
	var tempx = tempID[0];
	var tempy = tempID[1];
	var dirs = new Array();

	dirs.push('#' + tempx + 'p' + String(parseInt(tempy) - 1));
	dirs.push('#' + String(parseInt(tempx) + 1) + 'p' + tempy);
	dirs.push('#' + tempx + 'p' + String(parseInt(tempy) + 1));
	dirs.push('#' + String(parseInt(tempx) - 1) + 'p' + tempy);
	for (var i = 0; i < dirs.length; i++)
	{
		if (dirs[i].search('-') == -1)
		{// check all dirs
			if ($(dirs[i]).css('background-color') == $(doneEle).css('background-color'))
			{
				if (!($(dirs[i]).hasClass('done')))
				{
					done.push(dirs[i]);
					updateDone();
				}
			}
		}
	}
	return;
}
function move(inputColor)
{
	if (lastMove == inputColor)
	{ // the just played this color, would waste a move
		return;
	}
	if (ingame)
	{ // if game has been started
		for (var i = 0; i < done.length; i++)
		{// remove all classes from done elements, add input color
			var param = 'pieces done ' + inputColor + ' ' + theSizeString;
			//alert(param);
			if ($(done[i]).hasClass('done'))
			{// element is in done and now has new color and 'pieces' 'done' and 'color'
				$(done[i]).removeClass();
				$(done[i]).addClass(param);
			}
		}
		for (var i = 0; i < done.length; i++)
		{
			colorsMatch(done[i]);
		}
		updateDone();
		moveCount++;
		lastMove = inputColor;
		checkMoves();
	}
}
function checkMoves()
{
	$('#playermoves').html(moveCount);
	if (done.length == (gamesize * gamesize) && (moveCount <= maxMoves))
	{ // flooded entire board
		displayr(1);
		return;
	}
	if (moveCount == maxMoves)
	{// game is over : lose
		displayr(0);
	}
}
function displayr(state)
{// shows msgbox and overlay background	
	$('#fade').fadeIn('slow');
	$('#msgbox').slideToggle('slow');
	if (state == 0)
	{ // lose
		var text = 'I\'m sorry you ran out of moves.</br></br>';
		$('#msgbox p').html(text);
	}
	else if (state == 1)
	{ // win
		var text = moveCount + ''; // hack toString
		text = 'Nice job! You flooded the board in ' + text + ' moves!<br /><br />';
		$('#msgbox p').html(text);
	}
}
function hider()
{// hides msgbox and overlay background	
	$('#msgbox').slideToggle('slow');
	$('#fade').fadeOut('slow');
}
// gameplay ***************************************

// options code ***********************************
function showOptions()
{
	$('#options').slideToggle('slow');
	$('#fade').fadeIn('slow');
}
function changeSize(size)
{
	killBoard();
	startGame(size);
	$('#options').slideToggle('slow');
	$('#fade').fadeOut('slow');

/*$('#ajaxmeCont').addClass('ajaxmeBOT').removeClass('ajaxmeTOP');
$('.ajaxmeBOT').css('background-position', param);
}
else {
$('#ajaxmeCont').addClass('ajaxmeTOP').removeClass('ajaxmeBOT');
$('.ajaxmeTOP').css('background-position', param);
}*/
}
// options code ***********************************
